package com.we823.cms.controller.manage.sys;

import javax.servlet.http.HttpServletRequest;

import com.github.pagehelper.PageInfo;
import com.we823.cms.common.web.pageable.PageRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.we823.cms.common.WebConstants;
import com.we823.cms.common.utils.RequestUtil;
import com.we823.cms.controller.manage.AbstractManageController;
import com.we823.cms.model.cms.PhotoLink;
import com.we823.cms.repository.service.cms.PhotoLinkService;

@Controller
@RequestMapping(WebConstants.MANAGE_URL+"/photo_link")
public class PhotoLinkManageController extends AbstractManageController<PhotoLink, Integer> {

	private String baseUrl = WebConstants.MANAGE_URL+"/photo_link/";
	
	private PhotoLinkService photoLinkService;
	
	@Autowired
	public PhotoLinkManageController(PhotoLinkService photoLinkService) {
		super(photoLinkService);
		super.baseUrl=baseUrl;
		super.entityName="photolink";
		this.photoLinkService=photoLinkService;
		
	}

	@Override
	public PageInfo<PhotoLink> getData(PageRequest pageRequest,
									   HttpServletRequest request, Model model, boolean pageable) {
		PageInfo<PhotoLink> photolinkPage;
		String category = RequestUtil.getValueByName(request, "category");
		if (StringUtils.isEmpty(category)) {
			if (pageable) {
				photolinkPage = photoLinkService.findAll(pageRequest);
			}else {
				photolinkPage = new PageInfo<>(photoLinkService.findAll(getSort(request)));
			}
		}else {
			if (pageable) {
				photolinkPage = photoLinkService.findByCategory(category, pageRequest);
			}else {
				photolinkPage = new PageInfo<>(photoLinkService.findByCategory(category, getSort(request)));
			}
		}
		
		return photolinkPage;
	}

	@Override
	public void setIndetity() {
		setResourceIdentity("sys:photo_link");
	}

}
